package com.xzr.hospitalregister.util;

import android.content.Context;

import java.lang.reflect.Method;

/**
 * 系统参数获取
 *
 * @author Administrator
 *
 */

public class SystemPropertiesInvoker {

    /**
     * 根据给定Key获取值.
     *
     * @return 如果不存在该key则返回空字符串
     * @throws IllegalArgumentException
     *             如果key超过32个字符则抛出该异常
     */
    public static String get(Context context, String key)
            throws IllegalArgumentException {
        String ret = "";
        try {
            ClassLoader cl = context.getClassLoader();
            @SuppressWarnings("rawtypes")
            Class SystemProperties = cl
                    .loadClass("android.os.SystemProperties");
            // 参数类型
            @SuppressWarnings("rawtypes")
            Class[] paramTypes = new Class[1];
            paramTypes[0] = String.class;
            Method get = SystemProperties.getMethod("get", paramTypes);
            // 参数
            Object[] params = new Object[1];
            params[0] = new String(key);
            ret = (String) get.invoke(SystemProperties, params);
        } catch (IllegalArgumentException iAE) {
            throw iAE;
        } catch (Exception e) {
            ret = "";
        }

        return ret;

    }

    public static String getSystemProperties(String key) {
        String value = "";
        Class<?> SystemPropertiesClass;

        try {
            SystemPropertiesClass = Class
                    .forName("android.os.SystemProperties");
            Object SystemPropertiesObject = SystemPropertiesClass.newInstance();

            Class[] args1 = new Class[1];
            args1[0] = String.class;

            Method getMethod = SystemPropertiesClass.getDeclaredMethod("get",
                    args1);
            getMethod.setAccessible(true);
//       String[] argments = new String[1];
//       argments[0] = key;

            value = (String) getMethod.invoke(SystemPropertiesObject, key);
        } catch (Exception e) {
            value = null;
            e.printStackTrace();
        }
        return value;
    }
}
